package com.wgl.express.UI;

import com.wgl.express.entity.Package;
import com.wgl.express.entity.Station;
import com.wgl.express.entity.User;
import com.wgl.express.service.Impl.PackageServiceImpl;
import com.wgl.express.service.Impl.StationServiceImpl;
import com.wgl.express.service.Impl.UserServiceImpl;
import com.wgl.express.service.PackageService;
import com.wgl.express.service.StationService;
import com.wgl.express.service.UserService;
import com.wgl.express.util.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.wgl.express.text.Text.*;

public class Deliver {
    private Util util;
    private Station belongsStation;
    List<Package> allunbundle;
    List<Package> unbundle;

    public void start() {
        util = Util.getUtil();

        loadPostInfo();

        eventLoop();
    }

    private void eventLoop() {
        boolean exit = false;
        int choose;

        while (!exit) {
            util.say(TITLE);
            util.say("所属网点：" + this.belongsStation.getStationname() + "\n");
            util.say("Hello! " + UserServiceImpl.currentUser.getUsername() + "\n");
            util.say(MENU_POST);
//            util.readLine();
            if ((choose = util.readInt()) == -1) continue;
            switch (choose) {
                case 0:
                    exit = true;
                    break;
                case 1:
                    buildPackage();
                    break;
                case 2:
                    bundlePackage();
                    break;
                case 3:
                    storagedPackage();
                    break;
                case 4:
                    placedPackage();
                    break;

            }
        }
    }

    private void bundlePackage() {
        unbundle = new ArrayList<>();
        PackageService service = new PackageServiceImpl(1);
        int frameCount = service.getFrameCount();
        allunbundle = new ArrayList<>();
        int current = 1;
        unbundle = service.getPackUnbundled();
        allunbundle.addAll(unbundle);

        int c;
        boolean exit = false;

        do {
            util.say(PACK_LIST);
            util.say("第 " + current + " 页 / 共 " + frameCount + " 页\n");
            util.say(LINE);
            unbundle.forEach((p) -> {
                util.showPackInfo(p, true);
            });
            util.say(MENU_UNBUNDLE);
            c = util.readInt();

            switch (c) {
                case 1:
                    if (current != frameCount) {
                        unbundle = service.getPackUnbundled();
                        current += 1;
                    }
                    break;
                case 2:
                    gotoBundle();
                    break;
                case 0:
                    exit = true;
                    break;
            }
        } while (!exit);
    }

    private void gotoBundle() {
        util.say("请输入快递单号：");
        int no = util.readInt();
        List<Package> collect = allunbundle.stream().filter((p) -> p.getPid() == no).collect(Collectors.toList());
        if (collect.size() != 1) {
            util.say("快递单号错误！\n");
            util.say(LINE);
            return;
        } else {
            util.say("===揽件===\n");
        }

        util.showPackInfo(collect.get(0), true);

        StationService stationService = new StationServiceImpl();
        List<Station> stations = stationService.selectAll();

        util.say("请输入运费：\n");
        float price = util.readFloat();
        util.say("目的站点：\n");
        stations.forEach((s) -> {
            util.say(s.getSid() + " : " + s.getStationname() + "\n");
        });
        util.say("请输入站点编号：\n");
        int selectedSid = 0;
        while (true) {
            int sid = util.readInt();
            if (sid==0) return;
            if (!(stations.stream().map(Station::getSid).filter((s) -> sid == s).count() == 1)) {
                util.say("站点编号错误！\n");
                util.say("请输入站点编号(按0退出)：\n");

            } else {
                selectedSid = sid;
                break;
            }
        }


        UserService userService = new UserServiceImpl();
        List<User> postOfStation = userService.getPostOfStation(selectedSid);
        postOfStation.forEach((u) -> {
            util.say(u.getUid() + " : " + u.getUsername() + "\n");
        });
        util.say("请输入快递员编号：\n");
        int selectUid = 0;
        while (true) {
            int uid = util.readInt();
            if (uid==0) return;
            if (!(postOfStation.stream().map(User::getUid).filter((s) -> uid == s).count() == 1)) {
                util.say("快递员编号错误！\n");
                util.say("请输入快递员编号(按0退出)：\n");

            } else {
                selectUid = uid;
                break;
            }
        }

        util.say("确认提交？\n1.\t提交\t0.\t取消\n");
        int s = util.readInt();
        if (s == 1) {
            Package pack = collect.get(0);
            pack.setPrice(price);
            pack.setStatus(2);
            PackageService packageService = new PackageServiceImpl(1);
            int i = packageService.changePack(pack,false);
            int i1 = userService.insertPackToPost(selectUid, pack.getPid());
            util.say(LINE);
            if ((i +i1)== 2) {
                util.say("揽收成功！\n");
                unbundle.remove(pack);
                allunbundle.remove(pack);
            } else {
                util.say("揽收失败！\n");
            }
        }


    }

    private void buildPackage() {
        Package pack = new Package();
        int ch = -1;
        int res = 0;

        util.say(LINE);
        util.say(TITLE_NEW_PACKAGE);

        util.readLine();
        util.say("寄件人姓名：");
        pack.setFromname(util.readLine());
        util.say("寄件人号码：");
        pack.setFromphone(util.readLine());
        util.say("寄件人地址：");
        pack.setFromaddress(util.readLine());
        util.say("收件人姓名：");
        pack.setToname(util.readLine());
        util.say("收件人号码：");
        pack.setTophone(util.readLine());
        util.say("收件人地址：");
        pack.setToaddress(util.readLine());

        util.say(LINE);
        util.say("请确认寄件信息：" + "\n");
        util.say(LINE);

        util.showPackInfo(pack, true);

        util.say(LINE);

        do {
            util.say("确认信息无误？\n1. 确定\t0. 取消\t");
            ch = util.readInt();
            if (ch == 0) return;
        } while (ch == -1);

        PackageService packageService = new PackageServiceImpl(1);
        res = packageService.addPack(pack);
        if (res == 1) {
            util.say("新建包裹成功！");
        } else {
            util.say("新建包裹失败！");
        }


    }

    private void storagedPackage() {
        unbundle = new ArrayList<>();
        PackageService service = new PackageServiceImpl(2);
        int frameCount = service.getFrameCountOfPost(UserServiceImpl.currentUser.getUid());
        allunbundle = new ArrayList<>();
        int current = 1;
        unbundle = service.getPackStoraged(UserServiceImpl.currentUser.getUid());
        allunbundle.addAll(unbundle);

        int c;
        boolean exit = false;

        do {
            util.say(PACK_LIST);
            util.say("第 " + current + " 页 / 共 " + frameCount + " 页\n");
            util.say(LINE);
            unbundle.forEach((p) -> {
                util.showPackInfo(p, true);
            });
            util.say(MENU_STORAGE);
            c = util.readInt();

            switch (c) {
                case 1:
                    if (current != frameCount) {
                        unbundle = service.getPackStoraged(UserServiceImpl.currentUser.getUid());
                        current += 1;
                    }
                    break;
                case 2:
                    gotoStorage();
                    break;
                case 0:
                    exit = true;
                    break;
            }
        } while (!exit);
    }

    private void gotoStorage() {
        util.say("请输入快递单号：");
        int no = util.readInt();
        List<Package> collect = allunbundle.stream().filter((p) -> p.getPid() == no).collect(Collectors.toList());
        if (collect.size() != 1) {
            util.say("快递单号错误！\n");
            util.say(LINE);
            return;
        } else {
            util.say("===上架===\n");
        }

        util.showPackInfo(collect.get(0), true);

        util.say("请设置6位取件码：\n");
        String pickcode = String.valueOf(util.readInt());

        util.say("确认提交？\n1.\t提交\t0.\t取消\n");
        int s = util.readInt();
        if (s == 1) {
            Package pack = collect.get(0);
            pack.setStatus(3);
            pack.setPickcode(pickcode);
            PackageService packageService = new PackageServiceImpl(1);
            int i = packageService.changePack(pack,true);
            int i1 = packageService.changePack(pack,false);
            util.say(LINE);
            if ((i +i1)== 2) {
                util.say("上架成功！\n");
                unbundle.remove(pack);
                allunbundle.remove(pack);
            } else {
                util.say("上架失败！\n");
            }
        }
    }

    private void placedPackage() {
        unbundle = new ArrayList<>();
        PackageService service = new PackageServiceImpl(3);
        int frameCount = service.getFrameCountOfPost(UserServiceImpl.currentUser.getUid());
        allunbundle = new ArrayList<>();
        int current = 1;
        unbundle = service.getPackStoraged(UserServiceImpl.currentUser.getUid());
        allunbundle.addAll(unbundle);

        int c;
        boolean exit = false;

        do {
            util.say(PACK_LIST);
            util.say("第 " + current + " 页 / 共 " + frameCount + " 页\n");
            util.say(LINE);
            unbundle.forEach((p) -> {
                util.showPackInfo(p, true);
            });
            util.say(MENU_PLACED);
            c = util.readInt();

            switch (c) {
                case 1:
                    if (current != frameCount) {
                        unbundle = service.getPackStoraged(UserServiceImpl.currentUser.getUid());
                        current += 1;
                    }
                    break;
                case 2:
                    gotoPlaced();
                    break;
                case 0:
                    exit = true;
                    break;
            }
        } while (!exit);
    }

    private void gotoPlaced() {
        util.say("请输入快递单号：");
        int no = util.readInt();
        List<Package> collect = allunbundle.stream().filter((p) -> p.getPid() == no).collect(Collectors.toList());
        if (collect.size() != 1) {
            util.say("快递单号错误！\n");
            util.say(LINE);
            return;
        } else {
            util.say("===取件===\n");
        }

        util.showPackInfo(collect.get(0), true);

        util.say("确认取件？\n1.\t提交\t0.\t取消\n");
        int s = util.readInt();
        if (s == 1) {
            Package pack = collect.get(0);
            pack.setStatus(4);
            PackageService packageService = new PackageServiceImpl(1);
            int i1 = packageService.changePack(pack,false);
            util.say(LINE);
            if (i1 ==1) {
                util.say("取件成功！\n");
                unbundle.remove(pack);
                allunbundle.remove(pack);
            } else {
                util.say("取件失败！\n");
            }
        }
    }



    private void loadPostInfo() {
        StationService service = new StationServiceImpl();
        this.belongsStation = service.selectByDeliver(UserServiceImpl.currentUser.getUid());
    }


}
